import 'dart:async';
import 'package:flutter/material.dart';

class CountTimeButton extends StatefulWidget {
  final String text;
  final int countdownTime;
  final VoidCallback onPressed;

  const CountTimeButton({
    Key? key,
    required this.text,
    required this.countdownTime,
    required this.onPressed,
  }) : super(key: key);

  @override
  _CountdownButtonState createState() => _CountdownButtonState();
}

class _CountdownButtonState extends State<CountTimeButton> {
  late Timer _timer;
  late Stream<int> _stream;
  late int _countdownTime;

  @override
  void initState() {
    super.initState();
    _countdownTime = widget.countdownTime;
    _stream = Stream.periodic(Duration(seconds: 1), (int i) => i).take(_countdownTime);
    startTimer();
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  void startTimer() {
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      if (_countdownTime == 0) {
        timer.cancel();
      } else {
        setState(() {
          _countdownTime--;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<int>(
      stream: _stream,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.active) {
          final int remainingTime = _countdownTime - snapshot.data!;
          final String buttonText = remainingTime > 0 ? widget.text + '($_countdownTime 秒)' : widget.text;
          final bool isButtonEnabled = remainingTime <= 0;

          if (isButtonEnabled) {
            return ElevatedButton(
              onPressed: widget.onPressed,
              child: Text(widget.text),
              style: ElevatedButton.styleFrom(
                foregroundColor: Colors.white,
                backgroundColor: Colors.blue,
                minimumSize: Size(360, 60),
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
              ),
            );
          } else {
            return ElevatedButton(
              onPressed: null,
              child: Text(buttonText),
              style: ElevatedButton.styleFrom(
                foregroundColor: Colors.white,
                backgroundColor: Colors.blue,
                minimumSize: Size(360, 60),
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(10)),
              ),
            );
          }
        } else {
          return ElevatedButton(
            onPressed: null,
            child: Text(widget.text + '(${widget.countdownTime} 秒)'),
            style: ElevatedButton.styleFrom(
              foregroundColor: Colors.white,
              backgroundColor: Colors.blue,
              minimumSize: Size(360, 60),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10)),
            ),
          );
        }

        // else {
        //   return ElevatedButton(
        //     onPressed: null,
        //     child: Text(widget.text),
        //   );
        // }
      },
    );
  }
}
